package com.king.edu.interceptor;

import com.king.edu.utils.SystemConstant;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @ClassName: LoginIntercepter
 * @Description: 登录拦截器
 * @Author: wangchangyong
 * @Date: 2020/4/16 13:25
 * @Version: 1.0.0
 **/
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        String userSession = (String) session.getAttribute(SystemConstant.USER_SESSION_KEY);
        if (!StringUtils.isEmpty(userSession) && userSession.equals(SystemConstant.USERNAME)) {
            return true;
        }
        response.sendRedirect("/login");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
